<?php
namespace common\components;

use Yii;
use yii\base\Behavior;
use yii\web\Request;
use yii\base\Application;

class MyRequestBehavior extends Behavior
{
    private $_startTime;

    public function events()
    {
        return [
            Application::EVENT_BEFORE_REQUEST => 'beforeRequest',
            Application::EVENT_AFTER_REQUEST => 'afterRequest',
        ];
    }

    public function beforeRequest($event)
    {
        $this->_startTime = microtime(true);
        $hasError = false;
        if ($hasError) {
            // stop propagating EVENT_BEFORE_REQUEST
            $event->handled = true;
        }
    }

    public function afterRequest($event)
    {
        $time = microtime(true) - $this->_startTime;
        Yii::error("MyRequestBehavior spent $time second.");
    }
}